package com.fone.player.play.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.activity.BaseFragment;
import com.fone.player.activity.search.SearchActivity;
import com.fone.player.activity.search.SearchResultActivity;
import com.fone.player.client.VgdetailRst;
import com.fone.player.client.VgdetailRst.Info;
import com.fone.player.play.FonePlayerActivity;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.ScreenUtil;
import com.fone.player.view.AutoWrapViewGroup;

/**
 * 视频简介
 * 
 * @author kongxiaojun
 * @since 2014-4-18
 * 
 */
@SuppressLint("ValidFragment")
public class VideoIntroductionFragment extends BaseFragment implements View.OnClickListener {

	private View contentView;

	/**
	 * 视频名称、评分、年份、地区、类型、详情
	 */
	private TextView videoName, videoScore, videoYear, videoRegion, videoType, videoDesc;
	/**
	 * 导演1，导演2
	 */
	private TextView videoDirector1, videoDirector2;
	/** 主演viewgroup，可以自动换行 */
	private AutoWrapViewGroup starViewGroup;

	public VideoIntroductionFragment() {
		super();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		contentView = inflater.inflate(R.layout.video_itroduction, null);
		initView(((FonePlayerActivity) getActivity()).getVgdetailRst());
		return contentView;
	}

	private void initView(VgdetailRst videoDetail) {
		videoName = (TextView) contentView.findViewById(R.id.video_itroduction_video_name);
		videoScore = (TextView) contentView.findViewById(R.id.video_itroduction_video_score);
		videoYear = (TextView) contentView.findViewById(R.id.video_itroduction_video_year);
		videoRegion = (TextView) contentView.findViewById(R.id.video_itroduction_video_region);
		videoType = (TextView) contentView.findViewById(R.id.video_itroduction_video_type);
		videoDesc = (TextView) contentView.findViewById(R.id.video_itroduction_video_plot);
		videoDirector1 = (TextView) contentView.findViewById(R.id.video_itroduction_video_director1);
		videoDirector2 = (TextView) contentView.findViewById(R.id.video_itroduction_video_director2);
		starViewGroup = (AutoWrapViewGroup) contentView.findViewById(R.id.vg_start);
		// videoStaring1 = (TextView)
		// contentView.findViewById(R.id.video_itroduction_video_starring1);
		// videoStaring2 = (TextView)
		// contentView.findViewById(R.id.video_itroduction_video_starring2);
		// videoStaring3 = (TextView)
		// contentView.findViewById(R.id.video_itroduction_video_starring3);

		starViewGroup.removeAllViews();
		TextView starTitle = new TextView(getActivity());
		starTitle.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
		starTitle.setText("主演：");
		starTitle.setTextColor(getResources().getColor(R.color.video_detail_normal_textcolor));
		starTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.video_itroduction_normal_textsize));
		starViewGroup.addView(starTitle);

		if (videoDetail == null) {
			return;
		}

		if (!TextUtils.isEmpty(videoDetail.score)) {
			videoScore.setText(FoneUtil.formatNumber(Double.parseDouble(videoDetail.score) * 2, "0.0"));
			videoScore.setWidth(ScreenUtil.dp2px(30));
			if (videoDetail.name.length() > 16) {
				videoName.setWidth(ScreenUtil.getScreenWidthPix(getActivity()) - ScreenUtil.dp2px(70));
			}
		} else {
			videoScore.setText(R.string.video_no_score);
			videoScore.setWidth(ScreenUtil.dp2px(80));
			if (videoDetail.name.length() > 17) {
				videoName.setWidth(ScreenUtil.getScreenWidthPix(getActivity()) - ScreenUtil.dp2px(100));
			}
		}
		videoName.setText(videoDetail.name);
		if (videoDetail.infos != null && videoDetail.infos.infoList != null) {
			for (int i = 0; i < videoDetail.infos.infoList.size(); i++) {
				Info info = videoDetail.infos.infoList.get(i);
				// 显示导演
				if (info.k.contains("导演")) {
					if (info.vList != null) {
						for (VgdetailRst.V v : info.vList) {
							if (videoDirector1.getVisibility() == View.GONE) {
								videoDirector1.setText(v.name);
								videoDirector1.setVisibility(View.VISIBLE);
							} else if (videoDirector2.getVisibility() == View.GONE) {
								videoDirector2.setText(v.name);
								videoDirector2.setVisibility(View.VISIBLE);
							}
						}
					} else { // 导演是空的
						videoDirector1.setText(getString(R.string.player_unkonw));
						videoDirector1.setVisibility(View.VISIBLE);
					}
				} else if (info.k.contains("主演")) {
					if (info.vList != null) {
						for (VgdetailRst.V v : info.vList) {
							TextView tv = new TextView(getActivity());
							initStartTextView(tv, v.name);
							tv.setText(v.name);
							starViewGroup.addView(tv);
							starViewGroup.invalidate();
						}
					} else {// 主演是空的
						TextView tv = new TextView(getActivity());
						initStartTextView(tv, getString(R.string.player_unkonw));
						tv.setText(getString(R.string.player_unkonw));
						starViewGroup.addView(tv);
						starViewGroup.invalidate();
					}
				} else if (info.k.contains("类型")) {
					StringBuffer vtype = new StringBuffer();
					vtype.append("类型：");
					if (info.vList != null) {
						for (VgdetailRst.V v : info.vList) {
							vtype.append(v.name + " ");
						}
						videoType.setText(vtype.toString());
					}
				} else if (info.k.contains("地区")) {

					StringBuffer vRrgion = new StringBuffer();
					vRrgion.append("地区：");
					if (info.vList != null) {
						for (VgdetailRst.V v : info.vList) {
							vRrgion.append(v.name);
						}
						videoRegion.setText(vRrgion.toString());
					}
				}
			}
		}
		String year = TextUtils.isEmpty(videoDetail.year) ? getString(R.string.player_unkonw) : videoDetail.year;
		videoYear.setText("年份：" + year);
		String desc = TextUtils.isEmpty(videoDetail.desc) ? getString(R.string.player_unkonw) : videoDetail.desc;
		videoDesc.setText("剧情简介：" + desc);

		videoDirector1.setOnClickListener(this);
		videoDirector2.setOnClickListener(this);
	}

	private void initStartTextView(TextView tv, String content) {
		tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
		tv.setPadding(0, 0, ScreenUtil.dp2px(10), 0);
		tv.setTextColor(getResources().getColor(R.color.video_info_tab_line));
		tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.video_itroduction_normal_textsize));
		tv.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (!FoneUtil.isNetOkWithToast(getActivity())) {
			return;
		}
		if (v instanceof TextView) {
			TextView tv = (TextView) v;
			String name = tv.getText().toString();
			Intent intent = new Intent();
			intent.setClass(getActivity(), SearchResultActivity.class);
			intent.putExtra(SearchActivity.KeyWord, name);
			intent.putExtra(SearchActivity.SearchType, 1);
			startActivity(intent);
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		contentView = null;
		videoName = null;
		videoScore = null;
		videoYear = null;
		videoRegion = null;
		videoType = null;
		videoDesc = null;
		videoDirector1 = null;
		videoDirector2 = null;
		starViewGroup.removeAllViewsInLayout();
		starViewGroup = null;
	}

}
